package com.runfast.waimai.service.impl;

import com.runfast.common.service.BaseService;
import com.runfast.waimai.dao.mapper.RunfastBusinessMapper;
import com.runfast.waimai.dao.mapper.RunfastGoodsSellChildrenMapper;
import com.runfast.waimai.dao.mapper.RunfastGoodsSellMapper;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.RunfastGoodsSellService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RunfastGoodsSellServiceImpl extends BaseService<RunfastGoodsSell, Integer, RunfastGoodsSellExample> implements RunfastGoodsSellService {
   @Resource
    RunfastGoodsSellMapper dao;
   @Resource
    RunfastBusinessMapper businessDao;
   @Resource
    RunfastGoodsSellChildrenMapper goodsSellRecordChildrenDao;
    @Override
    public void updateSalesnum(RunfastGoodsSellRecord gr) {
        if(gr!=null&&gr.getBusinessid()!=null&&gr.getGoodstotal()!=null){
            RunfastBusiness bus=businessDao.findById(gr.getBusinessid());
            if(bus!=null){
                if(bus.getSalesnum()!=null){
                    bus.setSalesnum(bus.getSalesnum()+gr.getGoodstotal());

                }else{
                    bus.setSalesnum(gr.getGoodstotal());
                }
                businessDao.updateByPrimaryKeySelective(bus);
                List<RunfastGoodsSellChildren> list=goodsSellRecordChildrenDao.findbyorderid(gr.getId());
                if(list!=null&&list.size()>0){
                    for(RunfastGoodsSellChildren gsc:list){
                        if(gsc!=null&&gsc.getNum()!=null){
                            RunfastGoodsSell g=dao.get(gsc.getGoodssellid());
                            if(g!=null){
                                if(g.getSalesnum()!=null){
                                    g.setSalesnum(g.getSalesnum()+gsc.getNum());
                                }else{
                                    g.setSalesnum(gsc.getNum());
                                }
                                dao.updateSalesnum(g);
                            }

                        }

                    }
                }
            }
        }
    }

    @Override
    public RunfastGoodsSell get(Integer id) {
        return dao.get(id);
    }
}